package com.yunxin.core.infra.persistence.repository.order;

import cn.hutool.core.date.DateTime;
import com.yunxin.core.application.admin.team.DistributeDo;
import com.yunxin.core.application.command.order.OperateAllocationOrderRest;
import com.yunxin.core.domain.model.order.OrderUserApplyAllocation;
import com.yunxin.core.domain.model.order.OrderUserApplyAllocationRepository;
import com.yunxin.core.infra.persistence.converter.OrderConverter;
import com.yunxin.core.infra.persistence.entity.order.OrderUserApplyAllocationDO;
import com.yunxin.core.infra.persistence.mapper.order.OrderUserApplyAllocationExtendMapper;
import com.yunxin.core.utils.infra.support.request.PushOrderDTO;
import com.yunxin.core.utils.infra.utils.AesUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * @author zero°
 * @since 2023-07-12
 */
@Repository
@RequiredArgsConstructor
public class OrderUserApplyAllocationRepositoryImpl implements OrderUserApplyAllocationRepository {

    private final OrderUserApplyAllocationExtendMapper orderUserApplyAllocationExtendMapper;

    private final OrderConverter orderConverter;

    @Override
    public boolean saveOrderUserApplyAllocation(OrderUserApplyAllocation orderUserApplyAllocation) {
        OrderUserApplyAllocationDO orderUserApplyAllocationDO = orderConverter.formOrderUserApplyAllocation(orderUserApplyAllocation);
        int count = orderUserApplyAllocationExtendMapper.insertSelective(orderUserApplyAllocationDO);
        orderUserApplyAllocation.setId(orderUserApplyAllocationDO.getId());
        return count > 0;
    }

    @Override
    public int updateOrderUserApplyAllocation(OrderUserApplyAllocation orderUserApplyAllocation) {
        OrderUserApplyAllocationDO orderUserApplyAllocationDO = orderConverter.formOrderUserApplyAllocation(orderUserApplyAllocation);
        return orderUserApplyAllocationExtendMapper.updateByPrimaryKeySelective(orderUserApplyAllocationDO);
    }

    @Override
    public boolean updateAllocationOrderWechatById(Long id, String wechat) {
        int count = orderUserApplyAllocationExtendMapper.updateAllocationOrderWechatById(id, wechat);
        return count > 0;
    }

    @Override
    public boolean updateAllocationOrderEnterpriseWechatById(Long id, String enterpriseWechat) {
        int count = orderUserApplyAllocationExtendMapper.updateAllocationOrderEnterpriseWechatById(id, enterpriseWechat);
        return count > 0;
    }

    @Override
    public OrderUserApplyAllocation getOrderUserApplyAllocationById(Long id) {
        OrderUserApplyAllocationDO orderUserApplyAllocationDO = orderUserApplyAllocationExtendMapper.selectByPrimaryKey(id);
        return orderConverter.toOrderUserApplyAllocation(orderUserApplyAllocationDO);
    }

    @Override
    public int getAllocationOrderCountByProductId(Long productId, Date productStartTime, Date productEndTime) {
        return orderUserApplyAllocationExtendMapper.getAllocationOrderCountByProductId(productId, productStartTime, productEndTime);
    }

    @Override
    public OrderUserApplyAllocation getByApplyOrderNoAndAllocationTypes(String applyOrderNo, List<Integer> allocationTypes) {
        OrderUserApplyAllocationDO orderUserApplyAllocationDO = orderUserApplyAllocationExtendMapper.selectByApplyOrderNoAndAllocationTypes(applyOrderNo, allocationTypes);
        return orderConverter.toOrderUserApplyAllocation(orderUserApplyAllocationDO);
    }

    @Override
    public Long getAllocationIdByUserAndMerchant(String userCode, Long merchantId, Date startTime, Date endTime) {
        return orderUserApplyAllocationExtendMapper.getAllocationIdByUserAndMerchant(userCode, merchantId, startTime, endTime);
    }

    @Override
    public Long getAllocationOrderLastAccountIdByMerchant(Long merchantId,String code) {
        return orderUserApplyAllocationExtendMapper.getAllocationOrderLastAccountIdByMerchant(merchantId,code);
    }

    /**
     * 查询当前商户信息
     *
     * @param userId
     * @return
     */
    @Override
    public Long merchantIdByUserId(Long userId) {
        return orderUserApplyAllocationExtendMapper.merchantIdByUserId(userId);
    }


    @Override
    public int saveTeamTask(DistributeDo distributeDo) {
        return orderUserApplyAllocationExtendMapper.saveTeamTask(distributeDo);
    }

    /**
     * 查询任务列表
     * @return
     */
    @Override
    public List<DistributeDo> selectTeamTask(DateTime startTime,DateTime endTime) {
        return orderUserApplyAllocationExtendMapper.selectTeamTask(startTime,endTime);
    }

    /**
     * 根据订单查询订单详情
     *
     * @param orderIds
     * @return
     */
    @Override
    public List<OrderUserApplyAllocation> selectOrderDetail(List<Long> orderIds) {
        return orderUserApplyAllocationExtendMapper.selectOrderDetail(orderIds);
    }

    /**
     * 校验订单是否被分配
     *
     * @param orderIds
     * @return
     */
    @Override
    public List<Integer> selectalreadyDistribute(List<Long> orderIds) {
        return orderUserApplyAllocationExtendMapper.selectalreadyDistribute(orderIds);
    }


     /**
     * 根据订单号查询用户详情
     *
     * @param customerId
     * @return
     */
    @Override
    public PushOrderDTO selectOrderDetailByApplyOrderNo(Integer customerId) {
        PushOrderDTO pushOrderDTO = orderUserApplyAllocationExtendMapper.selectOrderDetailByApplyOrderNo(customerId);
        pushOrderDTO.setPhone(AesUtils.X.decrypt(pushOrderDTO.getPhone(), null));
        pushOrderDTO.setName(AesUtils.X.decrypt(pushOrderDTO.getName(), null));
        return pushOrderDTO;
    }

    @Override
    public Integer getAllocationOrderCountByMerchantId(Long merchantId, Date merchantStartTime, Date merchantEndTime) {
        return orderUserApplyAllocationExtendMapper.getAllocationOrderCountByMerchantId(merchantId, merchantStartTime, merchantEndTime);
    }
}
